/**
 * Copyright (C) 2024-2030 Super BilGen & 超亿时代信息技术股份有限公司.All Rights Reserved.
 */
package cn.com.yang.file;

/**
 * @Model cn.com.yang.thread.MultiThreadedFileProcessor
 * @Desc  TODO
 * @author yangyijin
 * @since 2025年5月21日-上午11:21:22
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import cn.com.yang.thread.ProcessLineThread;
 
public class MultiThreadedFileProcessor {
    private static final int THREAD_COUNT = 4; // 根据需要调整线程数
    private static final AtomicInteger lineCounter = new AtomicInteger(0);
    private static final String FILE_PATH = "path/to/your/large/file.txt";
 
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                int currentLine = lineCounter.getAndIncrement();
                int threadId = currentLine % THREAD_COUNT; // 分配线程ID
                executor.submit(new ProcessLineThread(line,threadId));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            executor.shutdown(); // 关闭线程池
        }
    }
    
//    private static void processLine(String line, int threadId) {
//        System.out.println("Thread " + threadId + " processing line: " + line);
//        // 在这里处理每一行数据
//    }
}
